// pagesEnterprise/pages/enterpriseSign/enterpriseSign.js
import ajax from "../../../utils/index"
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    /**
     * 表单校验是否为空
     */
    isSubmit: false,
    /**
     * 园区经纬度
     */
    parkLatitude: null,
    /**
     * 园区经纬度
     */
    parkLongitude: null,
    /**
     * 协议状态
     */
    agreement: false,
    /**
     * 表单对象
     */
    formData: {
      gardenId: null, // 园区ID
      gardenName: null, // 园区名称
      name: null, // 企业名称
      licenseImage: null, // 营业执照图片
      province: null, // 省份
      city: null, // 城市
      county: null, // 区/县
      address: null, // 详情地址
    },
    showLoginPicker: false,
  },
  loginSuccess() {
    this.loginCancel()
    this.getUserInfo()
  },
  loginCancel() {
    this.setData({ showLoginPicker: false })
  },
  isModeEvent() {
    this.setData({ showLoginPicker: true })
  },
  /**
   * 房产地址 事件
   */
  addressEvent() {
    const { formData } = this.data
    wx.chooseLocation({
      complete: (location) => ajax.getQQMap().reverseGeocoder({
        location: { latitude: location.latitude, longitude: location.longitude },
        success: (res) => {
          const { ad_info , address_component, address_reference } = res.result
          formData.adcode = ad_info.adcode
          formData.province = address_component.province, formData.city = address_component.city, formData.county = address_component.district, formData.town = address_reference.town.title, formData.address = address_reference.town.title + address_component.street_number + address_reference.landmark_l2.title;
          this.setData({ formData })
          this.isSubmitChange()
        }
      })
    })
  },
  /**
   * 协议事件
   */
  agreementChange(event) {
    this.data.agreement = event.detail
  },
  /**
   * 校验字段
   */
  isSubmitChange() {
    const { gardenId, name, licenseImage, province } = this.data.formData;
    const boo = !(gardenId && name && licenseImage && province)
    this.setData({ isSubmit: !boo })
  },
  /**
   * input 变化事件
   */
  formChange(event) {
    const { formData } = this.data
    const key = event.currentTarget.dataset.key
    const value = event.detail
    formData[key] = value
    this.setData({ formData })
    this.isSubmitChange()
  },
  /**
   * 搜索园区
   */
  formItemParkEvent () {
    const {formData} = this.data
    wx.navigateTo({
      url: '/pagesMine/pages/searchPark/searchPark',
      events: {
        backEvent: (event) => {
         (formData.gardenId = event.id,formData.gardenName = event.gardenName, formData.province = event.province, formData.city = event.city, formData.county = event.county, formData.address = event.address)
          this.setData({ parkLatitude: event.latitude, parkLongitude: event.longitude, formData });
          this.isSubmitChange()
        }
      },
    })
  },
  /**
   * 表单提交事件
   */
  async submitEvent () {
    if (!this.data.userInfo) return this.isModeEvent()
    if(!this.data.agreement) return ajax.showToast('请同意并阅读盈联易单配服务协议')
    ajax.showLoading();
    await ajax.post('/enterprise/add', this.data.formData).finally(() => wx.hideLoading());
    await ajax.showSuccess('操作成功');
    wx.navigateBack()
  },
  /**
   * 获取用户信息
   */
  async getUserInfo() {
    const userInfo = await ajax.getUserInfo({ isLogin: true })
    this.setData({ userInfo: userInfo })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.globalData.shareId = options.shareId || null
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getUserInfo()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})